package vek.XML.Parsers;

import java.util.ArrayList;
import org.xml.sax.ContentHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import vek.XML.parsedObjects.ParsedComercial;
import vek.XML.parsedObjects.ParsedProduct;

/**
 * parses the comercial part of the XML files, from < poicomercial > to < /poicomercial >.
 * @author Christian, Michal og Jakob (Hold 1, gruppe 2)
 */
public abstract class XMLParserComercial extends XMLParserAttractions implements ContentHandler {
    
    //parsing variables
    private boolean parsingComercialPOI;
    private boolean parsingProductCatalog;
    private boolean parsingTypesOfProductsSold;
    private boolean parsingProduct;
    
    //product catalog variables
    private String productName;
    private double productPrice;
    private String productType;
    private ArrayList<ParsedProduct> products = new ArrayList<ParsedProduct>();
    
    //types of products sold variables
    private ArrayList<String> typesOfProductsSold = new ArrayList<String>();
    
    protected ParsedComercial parsedComercial;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        
        super.startElement(uri, localName, qName, atts);
        
        if(localName.equals("comercialpoi"))
            {
                parsingComercialPOI = true;
            }
        if(parsingComercialPOI)
            {
                setTopElementStartTagsComercial(uri, localName, qName, atts);
            }
        
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {   
        
        super.endElement(uri, localName, qName);
        
        if(localName.equals("comercialpoi"))
            {
                parsingComercialPOI = false;
                parsedComercial = new ParsedComercial(parsedAttraction, 
                                        products, typesOfProductsSold);
                typesOfProductsSold = new ArrayList<String>();
            }
        if(parsingComercialPOI)
            {
                if(parsingTypesOfProductsSold)
                {
                    parseTypesOfProductsSold(uri, localName, qName);
                }
                else if(parsingProductCatalog)
                {
                    parseProductCatalog(uri, localName, qName);
                }
                setTopElementEndTagsComercial(uri, localName, qName);
            }
        
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        
       tempElementData = new String(ch, start, length);
        
    }
    
    /**
     * Sets the parts of the XML which the parser is parsing, when these tags occur, 
     * this parser knows that it should no longer encounter certain type of tags
     * @param uri not used
     * @param localName the name of the tag
     * @param qName not used
     * @throws org.xml.sax.SAXException
     */
    
    private void setTopElementEndTagsComercial(String uri, String localName, String qName) throws SAXException 
    {
        if(localName.equals("productcatalog"))
            {
                parsingProductCatalog = false;
            }
        if(localName.equals("typesofproductsold"))
            {
                parsingTypesOfProductsSold = false;
            }
        if(localName.equals("product"))
            {
                parsingProduct = false;
            }
    }
    
    /**
     * sets which parts of the XML file which the parser is currently parsing
     * @param uri not used
     * @param localName the name of the tag
     * @param qName not used
     * @param atts not used
     * @throws org.xml.sax.SAXException
     */
    
    private void setTopElementStartTagsComercial(String uri, String localName, String qName, Attributes atts) throws SAXException
    {
        if(localName.equals("productcatalog"))
            {
                parsingProductCatalog = true;
            }
        if(localName.equals("typesofproductsold"))
            {
                parsingTypesOfProductsSold = true;
            }
        if(localName.equals("product"))
            {
                parsingProduct = true;
            }
    }
    
    /**
     * Parses the Product Catalog part of the XML files, the information gets saved as a new ParsedProduct object.
     * @param uri
     * @param localName
     * @param qName
     * @throws org.xml.sax.SAXException
     */
    
    private void parseProductCatalog(String uri, String localName, String qName) throws SAXException
    { 
        if(localName.equals("product"))
            {
                products.add(new ParsedProduct(productName, productType, productPrice));
            }
        
        parseProduct(uri, localName, qName);
     
    }
    
    /**
     * Parses the products sold part of the XML file
     * @param uri not used
     * @param localName the name of the tag
     * @param qName not used
     * @throws org.xml.sax.SAXException
     */
    
    private void parseTypesOfProductsSold(String uri, String localName, String qName) throws SAXException
    {
        if(localName.equals("productsold"))
            {
                
                typesOfProductsSold.add(tempElementData);
            }    
    }
    
    /**
     * Parses the product part of the XML files, saves the information in variables
     * @param uri not used
     * @param localName the name of the tag
     * @param qName not used
     * @throws org.xml.sax.SAXException
     */
    
    private void parseProduct(String uri, String localName, String qName) throws SAXException
    {
        if(localName.equals("productname"))
            {
                productName = tempElementData;
            }
        else if(localName.equals("producttype"))
            {
                productType = tempElementData;
            }
        else if(localName.equals("productprice"))
            {
                productPrice = Double.parseDouble(tempElementData);
            }
    }
}
